package com.example.controller;

import com.example.common.result.Result;
import com.example.entity.dto.*;
import com.example.entity.hz.SFCTA;
import com.example.entity.vo.*;
import com.example.entity.vo.pg.PaiGongItemVo;
import com.example.mapper.MoctyMapper;
import com.example.service.MoctaService;
import com.example.service.SFCTEService;
import com.example.service.SfctaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "派工报工相关新接口前端控制器")
@RestController
@RequestMapping("/api/NewSfCte")
public class NewSfCteContronller {

    @Resource
    private MoctaService moctaService;
    @Resource
    private SfctaService sfctaService;
    @Resource
    private SFCTEService sfcteService;
    @Resource
    private MoctyMapper moctyMapper;

//    @ApiOperation("工单查询")
//    @GetMapping("/getMocta/{current}/{size}")
//    public Result getMocta(@PathVariable Integer current, @PathVariable Integer size, @RequestParam(required = false) String dh) {
//        Page<MoctaVo> page = PageHelper.startPage(current, size);
//        List<MoctaVo> list = moctaService.getMocta(dh);
//        Map<String, Object> map = MapUtils.getMap(page, list);
//        return Result.ok(map);
//    }

    @ApiOperation("分页查询工单信息(排产派工列表 工单级排产)")
    @PostMapping("/queryMocTaIPage/{current}/{size}")
    public Result<PageInfo<PaiGongItemVo>> queryMocTaIPage(@PathVariable Integer current, @PathVariable Integer size, @RequestBody MoctaDto moctaDto) {
        PageHelper.startPage(current,size);
        List<PaiGongItemVo> list = moctyMapper.getPGListFromMocty(moctaDto);
//        if (StringUtils.isNotEmpty(moctaDto.getStartTime()) || StringUtils.isNotEmpty(moctaDto.getEndTime())) {
//            list = moctyMapper.getPGListFromMocta(moctaDto);
//        }else {
//            list = moctyMapper.getPGListFromMocty(moctaDto);
//        }
        Map<String, List<ProcessVo>> map = new HashMap<>();
        List<ProcessVo> processVo = sfctaService.getProcess();
        for (ProcessVo vo : processVo) {
            String key = vo.getTA001().trim() + vo.getTA002().trim();
            if (!map.containsKey(key)) {
                map.put(key, new ArrayList<>());
            }
            map.get(key).add(vo);
        }
        for (PaiGongItemVo paiGongItemVo : list) {
            String key = paiGongItemVo.getWorkSingle().trim() + paiGongItemVo.getWorkNo().trim();
            if (map.containsKey(key)) {
                paiGongItemVo.setProcessVos(map.get(key));
            }
        }
        PageInfo<PaiGongItemVo> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @ApiOperation("点击工单通过工单的单别单号查询工单工艺信息")
    @GetMapping("/querySfCtaList/{single}/{odd}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "single", value = "工单单别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "odd", value = "工单单号", dataType = "String", dataTypeClass = String.class)
    })
    public Result<List<SFCTA>> querySfCtaList(@PathVariable String single,@PathVariable String odd){
        return this.sfctaService.querySfCtaList(single,odd);
    }

    @ApiOperation("新增派工单(新) 工序级")
    @PostMapping("/saveSfCTDandSFCTEprocess")
    public Result saveSfCTDandSFCTEprocess(@RequestBody List<SaveSfCTDandSFCTEprocessDto> sfctdList){
        return this.sfcteService.saveSfCTDandSFCTEprocess(sfctdList);
    }

    @ApiOperation("新增派工单(新)")
    @PostMapping("/saveSfcTdAndSfcTe")
    public Result saveSfcTdAndSfcTe(@RequestBody List<SfctdDto> sfctdList){
        return this.sfcteService.saveSfcTdAndSfcTe(sfctdList);
    }

    @ApiOperation("报工(扫描)")
    @GetMapping("/pgByUuid")
    public Result<List<SfcTeListVo>> pgByUuid(@RequestParam String db,@RequestParam String dh){
        List<SfcTeListVo> list = this.sfcteService.pgByUuid(db,dh);
        return Result.ok(list);
    }

    @ApiOperation("派工单分页查询(新)")
    @PostMapping("/querySfcTdIPage/{current}/{size}")
    public Result<Map<String,SfcTdVo>> querySfcTdIPage(@PathVariable Integer current, @PathVariable Integer size, @RequestBody SelectDto selectDto){
        return this.sfcteService.querySfcTdIPage(current,size,selectDto);
    }

    @ApiOperation("通过派工单号查询派工信息进行打印")
    @GetMapping("queryOneMocTa/{single}/{odd}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "single", value = "派工单单别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "odd", value = "派工单单号", dataType = "String", dataTypeClass = String.class)
    })
    public Result<MocTaAndSfcTeVo> queryOneMocTa(@PathVariable String single, @PathVariable String odd){
        return this.sfcteService.queryOneMocTa(single,odd);
    }

    @ApiOperation("汇总派工单显示")
    @GetMapping("queryMocTa")
    public Result<List<GroupVo>> queryMocta(){
        return sfcteService.queryMocta();
    }

    @ApiOperation("删除派工或报工单单据数据")
    @GetMapping("/deleteSfcTdAndSfcTe/{single}/{odd}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "single", value = "派/报工单单别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "odd", value = "派/报工单单号", dataType = "String", dataTypeClass = String.class)
    })
    public Result deleteSfcTdAndSfcTe(@PathVariable String single, @PathVariable String odd){
        return this.sfcteService.deleteSfcTdAndSfcTe(single,odd);
    }

    @ApiOperation("新增报工单(新)")
    @PostMapping("/saveSfcTdAndSfcTeBg")
    public Result saveSfcTdAndSfcTeBg(@RequestBody List<SfcTeListVo> sfcTeListVoList){
        return this.sfcteService.saveSfcTdAndSfcTeBg(sfcTeListVoList);
    }

    @ApiOperation("新增报工单(新) 扫描MOCTY")
    @PostMapping("/saveBGmocty")
    public Result saveBGmocty(@RequestBody SaveBGmoctyDto dto){
        return this.sfcteService.saveBGmocty(dto);
    }

    @ApiOperation("派工单-汇总")
    @PostMapping("/pgOrderList")
    public Result<List<PgListVo>> pgOrderList(@RequestBody MoctaDto moctaDto){
        List<PgListVo> pgOrderList = sfctaService.pgOrderList(moctaDto);
        return Result.ok(pgOrderList);
    }

}
